#include "theater.h"

void checkBathroom(int customerId, int groupId) {
    int temp;
    AcquireLock(lGroup[groupId]);
    addMV(answerBathroom[groupId]);
    /*  ask group mate if they want food */
    BroadcastCV(lGroup[groupId], cGroup[groupId]);
    /*  set monitor variable true */
    setMV(groupAskForBathroom[groupId] , true);
    /*  wait for all the customer answer for food */
    temp=getMV(answerBathroom[groupId]);
    if (temp != cr[customerId].groupSize)
    {
        WaitCV(lGroup[groupId], cGroup[groupId]);
    }
    ReleaseLock(lGroup[groupId]);
   /* check if head customer want to go to bathroom */
    if(answerForBathroom()){
        Printf("Customer [%d] in Group [%d] is going to the bathroom.\n",customerId,groupId);
        /*goBathroom(); */
        Printf("Customer [%d] in Group [%d] is leaving the bathroom.\n",customerId,groupId);
        Printf("Customer [%d] in Group [%d] is in the lobby.\n",customerId,groupId); 
    }else{
      	 Printf("Customer [%d] in Group [%d] is in the lobby.\n",customerId,groupId); 
    }
    AcquireLock( lGroup[groupId]);
    temp=getMV(numInGroupAskForBathroom[groupId]);
    if(temp!=0){
        WaitCV(lGroup[groupId], cGroup[groupId]);
    }
    ReleaseLock(lGroup[groupId]);
}

int  main()
{
    int debug;
    int customerId, groupId;
    int i;
    Customer * current;
    int headcrNeedBathroom=false;
    int temp;
    int temp1;
    initTheater();
    AcquireLock(cutomerNoLock);
    temp1=getMV(numGroups);
    for(i=0; i<temp1; i++)
    {
        temp=getMV(headCustomerInGroup[i]);
        if(-1==temp)
            break;
    }
    temp=getMV(nextCustomerId);
    setMV(headCustomerInGroup[i],temp);
    current=&cr[temp];
    customerId = temp;
    current->customerId=temp;
    groupId = i;
    current->groupId=i;
    current->groupSize=getMV(numCustomersInGroup[i]);
    setMV( nextCustomerId,++temp);
    Printf("customer %d is the head of group %d \n",current->customerId,current->groupId);
    ReleaseLock(cutomerNoLock);
    /*  ticket buyer */
    /* DEBUG('z', "\tGroup [%d] start buy tickets.\n", groupId) */;
    /* Head customer buy ticket for group member */
    buyTickets(customerId);
    /* DEBUG('z', "\tGroup [%d] finish buy tickets.\n", groupId) */;
    /* Head customer has buy the tickets and ask members to proceed to concession */
    proceed(customerId, groupId, groupTicket[groupId]);
    /* DEBUGINFO('c', "Customer [%d] in group [%d] finish waitGroup after finish buy tickets", customerId, groupId); */

    /* Head customer ask group member if they want food. If no food requirement,go to ticket taker and check ticket */
    if (countFood(customerId, groupId))
    {
        /* DEBUGINFO('c', "Customer [%d] in group [%d] start buy food", customerId, groupId); */
        /* Head customer buy food for group member. */
        buyFood(customerId);
    }
    /* DEBUGINFO('c', "Customer [%d] in group [%d] finish buy food", customerId, groupId); */
    /* Head customer has buy food and ask members to proceed to check ticket */
    proceed(customerId, groupId, groupFood[groupId]);
    /* DEBUGINFO('c', "Customer [%d] in group [%d] start check tickets.", customerId, groupId); */
    /* DEBUG('z', "\tGroup [%d] start \n", groupId) */;
    /* Head customer go to check ticket for group member */
    checkTickets(customerId);
    /* DEBUGINFO('c', "Customer [%d] in group [%d] finish check Ticket", customerId, groupId); */
    /* DEBUG('z', "\tGroup [%d] finish check tickets.\n", groupId) */;
    proceed(customerId, groupId, groupCheck[groupId]);
    /* Head customer have ticket checked for group member and ask member to enter theater room */
    /* DEBUGINFO('c', "Customer [%d] in group [%d] start take seat", customerId, groupId); */
    /* DEBUG('z', "\tGroup [%d] start take seat.\n", groupId) */;
    /* Head customer find seat for group member */
    arrangeSeats(customerId, groupId);
    /* DEBUGINFO('c', "Customer [%d] in group [%d] finish take seat", customerId, groupId); */
    /* DEBUG('z', "\tGroup [%d] finish take seat.\n", groupId) */;
    /* Head customer watch movie */
    watchMovie(customerId, groupId);
    /* DEBUGINFO('c', "Customer [%d] in group [%d] finish watch Movie", customerId, groupId); */
    /* Head customer ask group member to leave theater room */
    proceed(customerId, groupId, groupLeaveRoom[groupId]);
    /*    prtLeaveRoomMsg();*/
    Printf("Customer [%d] in group [%d] is getting out of a theater room seat.\n",customerId,groupId);
    checkBathroom(customerId, groupId);
    Printf("Customer [%d] in group [%d] bathroom over\n",customerId,groupId);
    proceed(customerId, groupId, groupLeaveTheater[groupId]);
    Printf("Customer [%d] in group [%d] leave the theater\n",customerId,groupId);
    AcquireLock(lCustomerLeft);	
    subMV(customerLeft); 
    ReleaseLock(lCustomerLeft);
    Exit(0);
}
/************************************************
 *
 *  end for head customer
 *
 ************************************************/

